﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

using Ext.Net;
using ProjectName.Data;
using ProjectName.Service;
using Kwan.Helper.Bitmaps;
using Kwan.Data;

namespace ProjectName.Web.Admin.TableName
{
    public partial class Default : UIPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                BtnAddClassInfo.Hidden = !permit[0];
                AppendChild.Hidden = !permit[0];
                BtnAddInfo.Hidden = !permit[0];
                Rename.Hidden = !permit[2];
                //btnChangeHot.Hidden = !permit[2];

                BtnDeleteInfo.Hidden = !permit[3];
                Remove.Hidden = !permit[3];
                if (permit[3])
                {
                    List<GridCommand> gridCommand = new List<GridCommand>{
                        ExtHelper.CreateGridCommand("Delete", Icon.Delete, "删除", "删除该条记录")
                    };
                    GridPanel1.ColumnModel.Columns.Add(ExtHelper.CreateCommandColumn(gridCommand));
                }
                //ResourceManager1.RegisterIcon(Icon.Image);
            }
        }

        protected void BtnAddInfo_Click(object sender, EventArgs e)
        {
            SetEditWindow(new TTableName());
            EditWindow.Show();
        }

        [DirectMethod]
        public void DeleteSelected(string obj)
        {
            string[] list = obj.Split(',');
            (TTableName.entityID == list).Delete<TTableName>();
            Store1.DataBind();
        }

        [DirectMethod]
        public void ChangeState(string obj, string NameValue)
        {
            IList<TTableName> list = JSON.Deserialize<IList<TTableName>>(obj);
            if (list.Count > 0)
            {
                CrudManage.ChangeState(list, NameValue);
                Store1.DataBind();
            }
            else
            {
                ExtHelper.GetNotification("系统提示", "请至少选择一项！").Show();
            }
        }

        [DirectMethod]
        public void EditInfo(string EntityID)
        {
            if (!permit[2]) return;
            SetEditWindow((TTableName.entityID == int.Parse(EntityID)).ListFirst<TTableName>());
            EditWindow.Show();
        }

        protected void RefreshDS(object sender, StoreRefreshDataEventArgs e)
        {
            NameValueCollection ps = new NameValueCollection();
            ps.Set("gridsort", e.Sort);
            ps.Set("dir", e.Dir.ToString());
            
            //搜索项
            ps.Set("Cid", txtFilter.Text);
            ps.Set("tag", txtSearch.Text);

            int pageindex = e.Start / e.Limit;
            Kwan.Data.Region r = new Kwan.Data.Region(pageindex, e.Limit);

            Expression exp = TableNameQuery.MakeSearchWhere(ps);
            IList<TTableName> ListView = exp.List<TTableName>(r, new string[] { e.Sort + " " + e.Dir });

            e.Total = new Expression().Count<TTableName>();
            Store1.DataSource = ListView;
            Store1.DataBind();
        }

        protected void SetEditWindow(TTableName tt)
        {
            txtID.Text = tt.EntityID.ToString();
            txtEnable.Checked = tt.Enable;
            txtHot.Checked = tt.Hot;;ExtSetInfo
        }

        protected void SaveEditWindow(object sender, DirectEventArgs e)
        {
            TTableName tt = Convert.ToInt32(txtID.Text) > 0 ? 
                (TTableName.entityID == Convert.ToInt32(txtID.Text)).ListFirst<TTableName>() : new TTableName();
ExtSaveInfo
            tt.Enable = txtEnable.Checked;
            tt.Hot = txtHot.Checked;

            DBContext.Save(tt);
            Store1.DataBind();
            Store1.Dispose();
            EditWindow.Hide();
        }
    }
}